
package com.starshineworks.util;

import java.util.ArrayList;
import java.util.List;

public class Menu {

    private MenuManager    mgr;
    private MenuTraits     traits;
    private Integer        rid;
    private Integer        mid;
    private List<String[]> options;
    private List<String[]> list;
    private Integer        numSel;
    private String         type;
    private String         varName;
    private String         style;
    
    public Menu(MenuManager m, Integer mi) {
        mgr        = m;
        rid        = m.rid;
        mid        = mi;
        traits     = m.traitList.get(mid - 1);
        varName    = traits.var + "r" + rid + "m" + mid;
        numSel     = traits.size;
        style      = traits.style;
        type       = traits.type;        
        options    = new ArrayList<String[]>();
        list       = mgr.getAvailableObjects(type, mi);

        for (Integer i = 1; i < list.size(); i++) {
            addOption(i, list.get(i));
        }
    }
    
    
    public String getVarName() {
        return this.varName;
    }
    
    public String getOptionIndex(Integer i) {
        return options.get(i)[0];
    }

    public String getOptionName(Integer i) {
        return options.get(i)[2];
    }

    public String getOptionValue(Integer i) {
        return options.get(i)[1];
    }
    
    public void addOption(Integer where, String[] e) {
        while (options.size() < (where + 1)) {
            String[] elm = {"0", "0", "----"};
            options.add(elm);
        }
        options.set(where, e);
    }
    
    String toHTML() {
        String y = "";
        
        if (style.equalsIgnoreCase("drop")) {
            String mult = "";
            Integer ini = 0;
            if (numSel > 1) {
                mult = " size="+numSel+"  ";// XXX multiple=multiple size="+numSel;
                ini = 1;
            } 
            y += "<select class=ssw_event menuId="+mid+" name="+getVarName()+"[] "+mult+" rowId="+rid+" >";
            for (Integer o = ini; o < options.size(); o++) {
                y += "<option textval='"+getOptionName(o)+ "' " +
                             "  value='"+getOptionName(o)+ "' " +
                             "  sswindex='"+o+"' >"+getOptionName(o)+"</option>";
            }
            y += "</select>";
        }
        
        if (style.equalsIgnoreCase("check")) {
            for (Integer o = 1; o < options.size(); o++) {
                y += "<input textval='"+getOptionName(o)+"' " + 
                             " value='"+getOptionName(o)+"' " +
                             " sswindex='"+o+"' " +
                             " class=ssw_event rowId="+rid+" menuId="+mid+" type=checkbox name="+getVarName()+"[] >"+getOptionName(o)+"</input><br/>";
            }
        }
        
        if (style.equalsIgnoreCase("radio")) {
            for (Integer o = 1; o < options.size(); o++) {
                y += "<input textval='"+getOptionName(o)+"' " + 
                        " value="+getOptionName(o)+" "+
                        " sswindex="+o+" "+
                        " class=ssw_event rowId="+rid+"  menuId="+mid+" type=radio "+
                        " name="+getVarName()+"[] >"+getOptionName(o)+"</input><br/>";
            }
        }
        
        return y;
    }

}
